package com.huijiet.controller;

import com.huijiet.base.common.CommonResult;
import com.huijiet.base.common.Ref;
import com.huijiet.base.component.GlobalParam;
import com.huijiet.base.utils.StringUtil;
import com.huijiet.param.NewsParam;
import com.huijiet.service.INewsService;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/api/news")
public class NewsController {

    @Resource
    private GlobalParam globalParam;
    @Resource
    private INewsService newsService;

    /**后台分页查询新闻列表*/
    @RequestMapping("/list")
    public CommonResult list(@Valid NewsParam.ListParam param){
        return newsService.list(param);
    }

    /**新闻封面图片上传*/
    @RequestMapping("/addImg")
    public CommonResult addImg(HttpServletRequest request){

        if(!(request instanceof MultipartHttpServletRequest)){
            System.out.println("不是文件上传");
            return Ref.error("不是文件上传");
        }
        MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
        MultiValueMap<String, MultipartFile> multiFileMap = req.getMultiFileMap();
        if(multiFileMap==null || multiFileMap.size()==0){
            return Ref.error("上传文件为空");
        }
        MultipartFile file = multiFileMap.getFirst("file");
        if(file==null){
            return Ref.error("上传文件为空");
        }
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        int i = originalFilename.lastIndexOf(".");
        String fileName = StringUtil.randomName() + originalFilename.substring(i);
        String filePath = globalParam.getImg().getImgLocation() + "/news/" + fileName;
        File imgFile = new File(filePath);
        File parentFile = imgFile.getParentFile();
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        try {
            file.transferTo(imgFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String imgUrl = "/upload/news/" + fileName;
        Map<String,String> map = new HashMap<>();
        map.put("id","101");
        map.put("imgUrl",imgUrl);
        map.put("prefix", globalParam.getImg().getPrefix());
        return Ref.ok(map);
    }

    /**新增新闻*/
    @RequestMapping("/add")
    public CommonResult add(@Valid NewsParam.AddParam param){

        return newsService.add(param);
    }

    /**修改新闻*/
    @RequestMapping("/modify")
    public CommonResult modify(@Valid NewsParam.ModifyParam param){
        return newsService.modify(param);
    }


    /**获取新闻详情*/
    @RequestMapping("/get")
    public CommonResult get(@Valid NewsParam.GetParam param){
        return newsService.get(param);
    }

    /**发布新闻*/
    @RequestMapping("/publish")
    public CommonResult publish(@Valid NewsParam.PublishParam param){
        return newsService.publish(param);
    }

    /**取消发布*/
    @RequestMapping("/cancel")
    public CommonResult cancel(@Valid NewsParam.CancelParam param){
        return newsService.cancel(param);
    }

    /**删除新闻*/
    @RequestMapping("/delete")
    public CommonResult delete(@Valid NewsParam.DeleteParam param){
        return newsService.delete(param);
    }


    /**前端获取新闻列表*/
    @RequestMapping("/pageList")
    public CommonResult pageList(@Valid NewsParam.PageListParam param){
        return newsService.pageList(param);
    }
}
